package com.example.t470p.facechecktickets.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.format.Time;
import android.widget.Toast;

public class util_info{
	public static void viewDialog(Context context, String _strMsg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示信息");
		builder.setMessage(_strMsg);
		//log.debug("show alertDialog:"+_strMsg);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		builder.create().show();
	}

	 public static String trimSpaces(String IP){//去掉IP字符串前后所有的空格 
	        while(IP.startsWith(" ")){ 
	               IP= IP.substring(1,IP.length()).trim(); 
	            } 
	        while(IP.endsWith(" ")){ 
	               IP= IP.substring(0,IP.length()-1).trim(); 
	            } 
	        return IP; 
	    } 
	 public static boolean isIp(String IP){//判断是否是一个IP 
	        boolean b = false; 
	        IP = trimSpaces(IP); 
	        if(IP.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){ 
	            String s[] = IP.split("\\."); 
	            if(Integer.parseInt(s[0])<255) 
	                if(Integer.parseInt(s[1])<255) 
	                    if(Integer.parseInt(s[2])<255) 
	                        if(Integer.parseInt(s[3])<255) 
	                            b = true; 
	        } 
	        return b; 
	    }

	public static String getNow() {
		try {
			Time mytoday = new Time();
			mytoday.setToNow();
			return mytoday.format("%H:%M:%S");
		} catch (Exception e) {
			e.printStackTrace();

			return "";
		}
	}
	 
	 public static long ip2Long(String strip) {
		   // 将127.0.0.1 形式的ip地址转换成10进制整数，这里没有进行任何错误处理
		   long[] ip = new long[4];
		   int position1 = strip.indexOf(".");
		   int position2 = strip.indexOf(".", position1 + 1);
		   int position3 = strip.indexOf(".", position2 + 1);
		   ip[0] = Long.parseLong(strip.substring(0, position1));
		   ip[1] = Long.parseLong(strip.substring(position1 + 1, position2));
		   ip[2] = Long.parseLong(strip.substring(position2 + 1, position3));
		   ip[3] = Long.parseLong(strip.substring(position3 + 1));
		   return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; // ip1*256*256*256+ip2*256*256+ip3*256+ip4
		} 
	 
	 public static byte[] ip2Byte(String strip) {
			   // 将127.0.0.1 形式的ip地址转换成10进制整数，这里没有进行任何错误处理
			   long[] ip = new long[4];
			   byte ret[]=new byte[4];
			   int position1 = strip.indexOf(".");
			   int position2 = strip.indexOf(".", position1 + 1);
			   int position3 = strip.indexOf(".", position2 + 1);
			   ip[0] = Long.parseLong(strip.substring(0, position1));
			   ip[1] = Long.parseLong(strip.substring(position1 + 1, position2));
			   ip[2] = Long.parseLong(strip.substring(position2 + 1, position3));
			   ip[3] = Long.parseLong(strip.substring(position3 + 1));
			   //return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; // ip1*256*256*256+ip2*256*256+ip3*256+ip4
			   ret[0]=(byte) (ip[0]&0xff);
			   ret[1]=(byte) (ip[1]&0xff);
			   ret[2]=(byte) (ip[2]&0xff);
			   ret[3]=(byte) (ip[3]&0xff);
			   return ret;
			}
	 
	 public static  void BigEndianInt2byte(int num,byte array[],int offset){
			array[offset+3]  =(byte) (num&0xff);
			array[offset+2]	=(byte) ((num>>8)&0xff);
			array[offset+1]	=(byte) ((num>>16)&0xff);
			array[offset]	=(byte) ((num>>24)&0xff);
		}
	 
	 public static void int2byteArray(int num,byte array[],int offset)
		{
			array[offset]  =(byte) (num&0xff);
			array[offset+1]=(byte) ((num>>8)&0xff);
			array[offset+2]=(byte) ((num>>16)&0xff);
			array[offset+3]=(byte) ((num>>24)&0xff);
		}
		
	public static int byteArray2int(byte array[],int offset)
		{
			return array[offset]&0xff|(array[offset+1]<<8)&0xff00|(array[offset+2]<<16)&0xff0000|(array[offset+3]<<24)&0xff000000;
		}
	public static void showToast(Activity a,String s){
		
		Toast.makeText(a, s, Toast.LENGTH_SHORT).show();
		return;
	}
	 
}